#! /usr/bin/env python3
import sys
import os

config={
    "net1":[("sw1","h1"),("sw4","h9")],
    "net2":[("sw10","h5"),("sw13","h4")],
    "net3":[("sw9","h6"),("sw8","h7"),("sw7","h8")],
    "R930":[("sw19","h2")],
}




def add_vhost_port(hostname="net1"):
    cmd =""
    for (sw,h) in config[hostname]:
        cmd += f'ovs-vsctl add-port {sw} {sw}-{h} -- set Interface {sw}-{h} type=dpdkvhostuserclient options:vhost-server-path=/tmp/{h}-{sw}.sock\n'
    print(cmd)
    return cmd

def config_vhost_port(hostname="net1"):
    cmd =""
    for (sw,h) in config[hostname]:
        cmd += f'ovs-vsctl set Interface {sw}-{h} type=dpdkvhostuserclient options:vhost-server-path=/tmp/{h}-{sw}.sock\n'
    print(cmd)
    return cmd

cmds = [
    add_vhost_port,
    config_vhost_port
]

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("请输入hostname")
        sys.exit(1)

    for i in range(len(cmds)):
        print(f'[{i}]: {cmds[i].__name__}')
    index = int(input("输入命令编号:"))
    os.popen(cmds[index](hostname=sys.argv[1]))
    